package ClientsAndHotels;

import java.util.Date;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;

/**
 * Klasa bazowa dla agenta, który odbiera komunikaty od kalendarza
 */
public abstract class CalendarObserverAgent extends BaseAgent {
    public static final String TYPE = "calendar_observer";

    @Override
    protected void setup() {
        registerOnYellowPages(TYPE, TYPE);
        addBehaviour(new CyclicBehaviour(this) {

            @Override
            public void action() {
                MessageTemplate mt = MessageTemplate
                        .and(MessageTemplate.and(MessageTemplate
                                .MatchPerformative(ACLMessage.INFORM),
                                MessageTemplate
                                        .MatchLanguage("JavaSerialization")),
                                MessageTemplate
                                        .MatchConversationId(CalendarAgent.NEW_DAY_CONVERSATION));
                ACLMessage msg = myAgent.receive(mt);
                if (msg != null) {
                    try {
                        onDateChanged((Date) msg.getContentObject());
                    } catch (UnreadableException e) {
                        e.printStackTrace();
                    }
                } else {
                    block();
                }
            }
        });
        super.setup();
    }

    protected abstract void onDateChanged(Date date);
}
